package com.bawei.lookfor2204a.view.activity.realname

import android.content.Intent
import coil.load
import com.bawei.lookfor2204a.R
import com.bawei.lookfor2204a.base.BaseActivity
import com.bawei.lookfor2204a.databinding.ActivityRealNameBinding
import com.blankj.utilcode.util.ToastUtils

/**
 * 实名认证页面
 */
class RealNameActivity : BaseActivity<RealNameViewModel, ActivityRealNameBinding>(R.layout.activity_real_name) {

    var sex = 1  // 性别

    override fun initData() {
        viewModel.realNameAuthenticationSuccess.observe(this, {
            ToastUtils.showShort(it.message)
        })
    }

    override fun initView() {
        // 返回
        binding.ivBack.setOnClickListener { finish() }
        // 选择身份证 证明 和 反面照片
        binding.ivIdcard1.setOnClickListener {
            startActivityForResult(Intent(Intent.ACTION_PICK).apply {
                type = "image/*"
            }, 1001)
        }
        binding.ivIdcard2.setOnClickListener {
            startActivityForResult(Intent(Intent.ACTION_PICK).apply {
                type = "image/*"
            }, 1002)
        }

        binding.rgSex.setOnCheckedChangeListener { radioGroup, i ->
            when(i) {
                R.id.rb_man -> sex = 1
                R.id.rb_woman -> sex = 2
            }
        }

        // 立即实名认证
        binding.btnRealName.setOnClickListener {
            val realName = binding.etRealName.text.toString()       // 真实姓名
            val idNumber = binding.etIdNumber.text.toString()       // 身份证号
            viewModel.realNameAuthentication(
                realName,
                sex,
                "1990-1-1",
                "北京八位温泉校区",
                idNumber,
                "北京市公安局",
                "9999-99-99"
            )

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 判断显示正面还是反面照片
        when(requestCode) {
            1001-> binding.ivIdcard1.load(data?.data)
            1002-> binding.ivIdcard2.load(data?.data)
        }
    }

    override fun getViewModel() = RealNameViewModel::class.java

}